<?php

declare(strict_types=1);

use Phinx\Db\Adapter\MysqlAdapter;
use think\migration\Migrator;

class CreateProductImportJobsTable extends Migrator
{
    public function change(): void
    {
        $this->table('product_import_job', [
            'comment' => '商品导入任务',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('seller_org_id', 'integer', [
                'signed' => false,
                'comment' => '卖家组织ID',
            ])
            ->addColumn('seller_user_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '提交人',
            ])
            ->addColumn('status', 'string', [
                'limit' => 20,
                'default' => 'pending',
                'comment' => 'pending|running|success|failed',
            ])
            ->addColumn('total_rows', 'integer', [
                'default' => 0,
                'comment' => '总数据量',
            ])
            ->addColumn('success_count', 'integer', [
                'default' => 0,
                'comment' => '成功条数',
            ])
            ->addColumn('fail_count', 'integer', [
                'default' => 0,
                'comment' => '失败条数',
            ])
            ->addColumn('chunk_size', 'integer', [
                'default' => 20,
                'comment' => '每批处理数量',
            ])
            ->addColumn('current_index', 'integer', [
                'default' => 0,
                'comment' => '已处理位置',
            ])
            ->addColumn('file_path', 'string', [
                'limit' => 255,
                'comment' => '导入文件路径',
            ])
            ->addColumn('file_hash', 'string', [
                'limit' => 64,
                'default' => '',
                'comment' => '文件哈希，用于去重',
            ])
            ->addColumn('payload_hash', 'string', [
                'limit' => 64,
                'default' => '',
                'comment' => '任务去重哈希',
            ])
            ->addColumn('mapping', 'text', [
                'limit' => MysqlAdapter::TEXT_REGULAR,
                'null' => true,
                'comment' => '字段映射配置(JSON)',
            ])
            ->addColumn('options', 'text', [
                'limit' => MysqlAdapter::TEXT_REGULAR,
                'null' => true,
                'comment' => '额外选项(JSON)',
            ])
            ->addColumn('error_log', 'text', [
                'limit' => MysqlAdapter::TEXT_LONG,
                'null' => true,
                'comment' => '失败详情(JSON)',
            ])
            ->addColumn('started_at', 'datetime', [
                'null' => true,
                'comment' => '开始时间',
            ])
            ->addColumn('finished_at', 'datetime', [
                'null' => true,
                'comment' => '结束时间',
            ])
            ->addColumn('notify_status', 'string', [
                'limit' => 20,
                'default' => 'pending',
                'comment' => '通知状态 pending|sent',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '软删除',
            ])
            ->addIndex(['seller_org_id'])
            ->addIndex(['status'])
            ->addIndex(['notify_status'])
            ->addIndex(['file_hash'])
            ->addIndex(['payload_hash'])
            ->create();
    }
}
